package com.kkwrite.regimen.consumer.controller.collection;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.collection.CollectionDTO;
import com.kkwrite.regimen.consumer.controller.constant.Constant;

/** 
 * 收藏
 *
 * @author jinying
 * 
 */
@Api(value = "收藏")
@RestController
@RequestMapping("/collection")
public class CollectionController {

	private static final Logger logger = LoggerFactory.getLogger(CollectionController.class);
	
	@Resource(name = "restTemplate")
	private RestTemplate restTemplate;
	
	@Resource(name = "stringRedisTemplate")
	private StringRedisTemplate redisTemplate;
	
	@ApiOperation(value = "根据用户标识查询用户收藏")
	@ApiImplicitParams(
        {
            @ApiImplicitParam(name = "collectOwnerType", value = "收藏类型", paramType = "query", dataType = "Integer")
        }
    )
	@SuppressWarnings("rawtypes")
	@GetMapping("/getCollection")
	public OutDTO<List<CollectionDTO>> getCollection(HttpServletRequest request,@RequestParam Integer collectOwnerType) {
		String token = request.getHeader("token");
		String userId = (String)redisTemplate.opsForHash().get(token, "userId");
		OutDTO outDTO = null;
		try {
			String url = Constant.SERVICE_PREFIX + "/collection/getCollection?userId=" +userId+"&collectOwnerType=" + collectOwnerType;
			outDTO = restTemplate.getForObject(url, OutDTO.class);
			return outDTO;
		} catch (RestClientException e) {
			logger.error("查询收藏出错，userId = " + userId +";collectOwnerType="+collectOwnerType+ "_" + e.getMessage());
			e.printStackTrace();
			return new OutDTO<List<CollectionDTO>>("failed", e.getMessage());
		}
	}
}
